(function() {
    Nokia.CustomizableList = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				title: '',
				visible: false
			};

            instance.options = jQuery.extend(defaults, options);

            instance.element = jQuery( instance.options.element );

            instance._super.apply(instance, [instance.options]);

			instance.registerData("customizablelist");

			instance._create();

			instance.registerVibrationOn([ ]);

			instance.items = [];

			instance.itemsWrapperArray = []

			instance.fireCallback('create');
        },

		addItem: function(item) {
			var instance = this;
			var Styles = instance.Styles;
			var wrapper = Nokia.dom.parseHTML('<div></div>');
			var itemElement = jQuery(item);

			if (itemElement.length == 0 && typeof item == 'string') {
				wrapper.append(item);
			} else {
				Nokia.dom.append(wrapper, item);
			}

			Nokia.util.addClass(wrapper[0], [Styles.uiStateDefault, Styles.customizableListItem, Styles.uiHelperClearFix].join(' '));

			if (instance.itemsWrapper.is(':empty')) {
				Nokia.util.addClass(wrapper[0], Styles.customizableListItemFirst);
			}

			instance.itemsWrapperArray.push(wrapper);

			Nokia.dom.append(instance.itemsWrapper, wrapper);

			instance.items.push(item);

			instance.fireCallback('addItem', null, wrapper.children());
		},

		clean: function() {
			var instance = this;

			instance.items = [];
			instance.itemsWrapperArray = [];
			instance.itemsWrapper.html('');
		},

		removeItem: function(item) {
			var instance = this;
			var Styles = instance.Styles;

			var index = jQuery.inArray(item, instance.items);

			if (index > -1) {
				var itemElement = instance.itemsWrapperArray[index][0];
				Nokia.dom.remove(itemElement);

				instance.fireCallback('removeItem', null, index);
				Nokia.util.arrayRemove(instance.items, index);
			}

		},

		show: function() {
			var instance = this;
			var element = instance.element;

			Nokia.animation.fadeIn(element);

			instance.fireCallback('show', []);
		},

		hide: function() {
			var instance = this;
			Nokia.animation.fadeOut(instance.element);
			instance.fireCallback('hide');
		},

		_create: function() {
			var instance = this;
			var options = instance.options;
			var element = instance.element;
			var Styles = instance.Styles;

			instance.itemsWrapper = Nokia.dom.parseHTML('<div class="' + [Styles.customizableListItems].join(' ') + '"></div>');

			if (options.visible) {
				element[0].style.display = 'block';
			}

			Nokia.dom.append(element, instance.itemsWrapper);

			element.addClass( [Styles.customizableList, Styles.uiStateDefault, Styles.uiHelperClearFix].join(' ') );
		},

		Styles: {
			customizableList: 'nokia-customizablelist',
			customizableListTitle: 'nokia-customizablelist-title',
			customizableListItems: 'nokia-customizablelist-items',
			customizableListItem: 'nokia-customizablelist-item',
			customizableListItemFirst: 'nokia-customizablelist-item-first',
			uiStateDefault: 'ui-state-default',
			uiHelperClearFix: 'ui-helper-clearfix',
			uiWidgetHeader: 'ui-widget-header'
		}
    });

})();